package router

import (
	"crow-k8s/src/config"
	"crow-k8s/src/service"
	"github.com/gin-gonic/gin"
)

// GetDeploymentList 查看deployment列表
// @Summary 查看deployment列表
// @Description 查看deployment列表
// @Tags Deployment
// @Param namespace_name query string true "namespace名字"
// @Success 200 {object} response.Response{data=v1.DeploymentList} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/deployment/list [get]
// @Security Bearer
func GetDeploymentList(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	deploymentList, err := service.GetDeploymentList(config.K8sClientSet, namespaceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentList)
}

// GetDeployment 查看指定deployment
// @Summary 查看指定deployment
// @Description 查看指定deployment
// @Tags Deployment
// @Param namespace_name query string true "namespace名字"
// @Param deployment_name query string true "deployment名字"
// @Success 200 {object} response.Response{data=v1.Deployment} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/deployment [get]
// @Security Bearer
func GetDeployment(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	deploymentName := c.Query("deployment_name")
	deploymentInfo, err := service.GetDeployment(config.K8sClientSet, namespaceName, deploymentName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// CreateDeployment 创建deployment
// @Summary 创建deployment
// @Description 创建deployment
// @Tags Deployment
// @Accept  application/json
// @Product application/json
// @Param data body service.CreateDeploymentRequest true "创建deployment的信息"
// @Success 200 {object} response.Response{data=v1.Deployment} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/deployment [post]
// @Security Bearer
func CreateDeployment(c *gin.Context) {
	var request service.CreateDeploymentRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	deploymentInfo, err := service.CreateDeployment(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// DeleteDeployment 删除指定deployment
// @Summary 删除指定deployment
// @Description 删除指定deployment
// @Tags Deployment
// @Accept  application/json
// @Product application/json
// @Param data body service.DeleteDeploymentRequest true "删除deployment"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/deployment [delete]
// @Security Bearer
func DeleteDeployment(c *gin.Context) {
	var request service.DeleteDeploymentRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	err = service.DeleteDeployment(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}

// ApplyDeployment 修改指定deployment
// @Summary 修改指定deployment
// @Description 修改指定deployment
// @Tags Deployment
// @Accept  application/json
// @Product application/json
// @Param data body service.ApplyDeploymentRequest true "修改deployment"
// @Success 200 {object} response.Response{data=v1.Deployment} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/deployment [put]
// @Security Bearer
func ApplyDeployment(c *gin.Context) {
	var request service.ApplyDeploymentRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	deploymentInfo, err := service.ApplyDeployment(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// ApplyDeploymentByVolumes 修改deployment的挂载
// @Summary 修改deployment的挂载
// @Description 修改deployment的挂载
// @Tags Deployment
// @Accept  application/json
// @Product application/json
// @Param data body service.MountForDeploymentRequest true "修改deployment的挂载信息"
// @Success 200 {object} response.Response{data=v1.Deployment} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/deployment/volumes [put]
// @Security Bearer
func ApplyDeploymentByVolumes(c *gin.Context) {
	var request service.MountForDeploymentRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	deploymentInfo, err := service.MountVolumes(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// RollBackDeployment deployment回滚
// @Summary deployment回滚
// @Description deployment回滚
// @Tags Deployment
// @Accept  application/json
// @Product application/json
// @Param data body service.RollBackDeploymentRequest true "deployment回滚"
// @Success 200 {object} response.Response{data=v1.Deployment} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/deployment/version [put]
// @Security Bearer
func RollBackDeployment(c *gin.Context) {
	var request service.RollBackDeploymentRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	deploymentInfo, err := service.RollBackDeployment(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}
